<?php
/**
 * 释放
 */
namespace app\api\server;

use app\common\model\Currency as CurrencyModel;
use app\common\model\UserMoneyLog as UserMoneyLogModel;
use app\api\model\BaseConfig as BaseConfigModel;
use app\common\model\MemberPledge as MemberPledgeModel;
use app\common\model\MemberLevel as MemberLevelModel;
use app\common\model\Member as MemberModel;
use think\facade\Db;

class Release
{
    /**
     * 发放质押信息
     */
    public static function pledge(){
        //质押代币ID
        $pledge_cid = BaseConfigModel::config_data('pledge_cid');
        //释放质押代币ID
        $release_cid = BaseConfigModel::config_data('pledge_release_cid');
        //每日释放比例
        $pledge_num = BaseConfigModel::config_data('pledge_num');
        if($release_cid == 0) return false;
        if($pledge_num == 0) return false;

        $currency = CurrencyModel::getDetails($release_cid);

        if(empty($currency)) return false;

        $proportion = bcdiv($pledge_num,100,8);

        //查询质押列表
        $list = MemberPledgeModel::where('cid',$pledge_cid)->where('money','>',0)->select();
        foreach ($list as $v){
            //计算今日释放
            $release = bcmul($v['money'],$proportion,8);
            //统计总释放
            $v['release'] = bcadd($v['release'],$release,8);
            //释放
            UserMoneyLogModel::change($v['mid'],$currency['name'],$release,'Mining machine output',$v['id'],'每日矿机释放');
            //更新用户总释放
            MemberPledgeModel::where('id',$v['id'])->update(['release'=>$v['release']]);
        }
    }

    /**
     * 等级释放
     */
    public static function levelRelease(){
        //查询所有等级
        $level_list = MemberLevelModel::getMemberList();
        $lid = '';
        $level_daily = [];
        //获取释放ID
        foreach ($level_list as $l){
            if($l['daily_num'] > 0 && $l['daily_reward_cid'] > 0){
                $lid .= empty($lid) ? $l['id'] : ','.$l['id'];
                //记录会释放的用户
                $currency = CurrencyModel::getDetails($l['daily_reward_cid']);
                $l['daily_reward_name'] = $currency['name'];
                $level_daily[$l['id']] = $l;
            }
        }

        $page = 1;
        $list = MemberModel::where('level_id','in',$lid)->order('id desc')->paginate(['list_rows'=>200,'page'=>$page])->toArray();
        $last_page = $list['last_page'];    //最后一页

        //发放奖励
        foreach ($list['data'] as $m){
            UserMoneyLogModel::change($m['id'],$level_daily[$m['level_id']]['daily_reward_name'],$level_daily[$m['level_id']]['daily_num'],'Level rewards','','级别奖励');
        }

        //分页多次发放奖励
        if($last_page > $page){

            $page += 1;
            for ($page;$page<=$last_page;$page++){
                $list = MemberModel::where('level_id','in',$lid)->order('id desc')->paginate(['list_rows'=>200,'page'=>$page])->toArray();
                if(!empty($list['data'])){
                    foreach ($list['data'] as $m){
                        UserMoneyLogModel::change($m['id'],$level_daily[$m['level_id']]['daily_reward_name'],$level_daily[$m['level_id']]['daily_num'],'Level rewards','','级别奖励');
                    }
                }
            }

        }

    }

}